#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#pragma warning(disable:4996)

char *tmp = "NOTFOUND";

typedef struct node
{
    char url[100];      
    char ip[21][50];
    int num;
    int round_num;
    struct node* next;
}Node, * nodeptr;

//??????????
nodeptr create_list()           
{
    nodeptr first = (nodeptr)malloc(sizeof(Node));
    if(first!=NULL)
    {
        memset(first->url, '\0', sizeof(first->url));
        memset(first->ip, '\0', sizeof(first->ip));
        first->next = NULL;
        return first;
    }
    return NULL;   
}

//???????????????
nodeptr push_front(nodeptr head, char* url, char* ip)
{
    nodeptr new_first = (nodeptr)malloc(sizeof(Node));
    new_first->num=1;
    new_first->round_num=0;
    memset(new_first->ip,'\0',sizeof(new_first->ip));
    if(new_first==NULL)
    {
        return NULL;
    }
    if (!(strcpy(new_first->url, url) && strcpy(new_first->ip[0], ip)))
    {
        //printf("???????\n");
        exit(1);
    }

    new_first->next = head;

    return new_first;
}

//???????
void print_list(nodeptr head)
{
    while (head->next != NULL) {
        printf("%s  %s\n", head->url, head->ip);
        head = head->next;
    }
}

//????url???????????????????ip???
char* get_ip(nodeptr head, char* url)
{
    while (head->next != NULL)
    {
        if(strcmp(head->url,url)==0)
        {
            head->round_num%=head->num;
            return head->ip[(head->round_num++)%head->num];
        }
        head = head->next;
    }

    // char tmp[10] = "NOTFOUND";
    return tmp;
}

/*
//??????
nodeptr delete(nodeptr head,char *url)
{
    nodeptr tmp=head->next;
    if(strcmp(head->next->url,url)==0)
    {
        head->next=tmp->next;
        free(tmp);
        return head;
    }
    else
    {
        for(tmp=head; tmp->next!=NULL&&strcmp(url,tmp->next->url)!=0;tmp=tmp->next);
        if(tmp->next==NULL)
        {
            return head;
        }
        nodeptr des=tmp->next;
        tmp->next=des->next;
        free(des);
        return head;
    }
}
*/

//??????????????????
nodeptr pop_back(nodeptr head)
{
    if(head->next==NULL)
    {
        return head;
    }
    else
    {
        nodeptr prev=head,p=head;
        for(p=head;p->next->next!=NULL;prev=p,p=p->next);

        if(p==head)
        {
            nodeptr temp=p->next;
            free(p);
            return temp;
        }
        else
        {
            prev->next=p->next;
            free(p);
            return head;
        }
    }
}

//????????????
int size(nodeptr head)
{
    int cnt = 0;
    while (head->next)
    {
        cnt++;
        head = head->next;
    }
    return cnt;
}

//?????????????????
nodeptr move_to_head(nodeptr head,char* url)
{
    nodeptr prev=head,cur=head;
    while(cur->next!=NULL)
    {
        if(strcmp(cur->url,url)==0)
        {
            break;
        }
        prev=cur;
        cur=cur->next;
    }

    if(cur==head)
    {
        return head;
    }
    else
    {
        prev->next=cur->next;
        cur->next=head;
        return cur;
    }
}